package com.tjl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjl.model.Car;
import com.tjl.model.Star;
import com.tjl.service.Impl.CarServiceImpl;
import com.tjl.util.CodeMsg;
import com.tjl.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TJL
 * @date 2022/5/28
 */
@RestController
@RequestMapping("/cars")
public class CarController {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private CarServiceImpl service;

    @GetMapping
    public CodeMsg getALl(@RequestHeader String token){
        Claims claims = null;
        try {
            claims = jwtUtils.parseJwt(token);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg(202,"token异常");
        }
        QueryWrapper<Star> wrapper = new QueryWrapper<>();
        wrapper.like("uid",claims.getId());

        return new CodeMsg(service.selectCar(Integer.parseInt(claims.getId())));
    }

    @PostMapping
    public  CodeMsg add(@RequestHeader String token,@RequestBody Car car){
        Claims claims;
        try {
            claims = jwtUtils.parseJwt(token);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg(202,"token异常");
        }
        car.setUid(Integer.parseInt(claims.getId()));
        List<Car> list = service.list();
        //如果用户id 产品id 尺码相同 则变成加数量
        for (Car c: list) {
            System.out.println(c.getCid()+"===="+car.getCid()+"==="+(c.getCid() == car.getCarid()));
            if (c.getCid() == car.getCid() && c.getUid() == car.getUid()
                    && c.getShoesize() == car.getShoesize()){
                System.out.println("判断相同");
                car.setCarid(c.getCarid());

                car.setNum(c.getNum()+car.getNum());
                if (car.getNum() >= 10) {
                    car.setNum(10);
                }
                return new CodeMsg(service.updateById(car));
            }
        }
        return new CodeMsg(service.save(car));
    }

    @DeleteMapping("/{id}")
    public CodeMsg delete(@RequestHeader String token,@PathVariable Integer id){
        try {
            Claims claims = jwtUtils.parseJwt(token);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg(202,"token异常");
        }
        return new CodeMsg(service.removeById(id));
    }

    @PutMapping
    public  CodeMsg put(@RequestHeader String token,@RequestBody Car car){
        Claims claims;
        try {
            claims = jwtUtils.parseJwt(token);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg(202,"token异常");
        }

        car.setUid(Integer.parseInt(claims.getId()));

        return new CodeMsg(service.updateById(car));
    }
    @PostMapping("/deleteList")
    public CodeMsg deleteList(@RequestHeader String token,@RequestBody List<Integer> list){

        Claims claims;
        try {
            claims = jwtUtils.parseJwt(token);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg(202,"token异常");
        }
        return new CodeMsg(service.removeBatchByIds(list));
    }

}
